using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Xeml.Document;
using Mpi.Common.Collections;
using Xeml.Sampling.Contracts;

namespace Xeml.Sampling
{
    public class StandardTableFormatter : ITableFormatter
    {
        private List<ColumnEmitter> emitter;
        #region ITableFormatter Members

        public List<ColumnEmitter> Emitter
        {
            get { return this.emitter; }
        }

        public System.Data.DataTable Format(Xeml.Document.Contracts.IDocument document)
        {
            DataTable ret = new DataTable();
            foreach (ColumnEmitter em in this.emitter)
            {
                foreach (Column col in em.Columns.Values)
                {
                    AddAllSubCols(ret, col);
                }
            }
            foreach (TreeNode<StoryBase> storyNode in document.StoryBoard)
            {
                if (storyNode.Content.Samples.Count > 0)
                {
                    foreach (Sample s in storyNode.Content.Samples)
                    {
                        DataRow dr = ret.NewRow();

                        foreach (DataColumn dc in ret.Columns)
                        {
                            Column c = (Column)dc.ExtendedProperties[dc.ColumnName];

                            if (c is VariableColumn)
                                dr[dc] = c.ExtractRowValue(storyNode.Content);
                            if (c is PositionColumn)
                                dr[dc] = c.ExtractRowValue(s);
                            if (c is SampleColumn)
                                dr[dc] = c.ExtractRowValue(s);
                            if (c is EventColumn)
                                dr[dc] = c.ExtractRowValue(storyNode);
                            if (c is ObPointAnnotationColumn)
                            {
                                ObservationPoint op = storyNode.Content.ObservationPoints.Find(s.TimePoint);
                                if (op != null)
                                {
                                    dr[dc] = c.ExtractRowValue(op);
                                }



                            }
                        }
                        ret.Rows.Add(dr);
                    }
                }
                else
                {
                    DataRow dr = ret.NewRow();

                    foreach (DataColumn dc in ret.Columns)
                    {
                        Column c = (Column)dc.ExtendedProperties[dc.ColumnName];

                        if (c is VariableColumn)
                            dr[dc] = c.ExtractRowValue(storyNode.Content);

                        if (c is EventColumn)
                            dr[dc] = c.ExtractRowValue(storyNode);
                        //if (c is AnnotationColumn)
                        //{
                        //    ObservationPoint op = storyNode.Content.ObservationPoints.Find(s.Time);
                        //    if (op != null)
                        //    {
                        //        dr[dc] = c.ExtractRowValue(op);
                        //    }



                        //}
                    }
                    ret.Rows.Add(dr);
                }

            }
            return ret;
        }



        private void AddAllSubCols(DataTable dt, Column col)
        {
            if (!col.IsContainerColumn)
            {
                XemlDataColumn dc = new XemlDataColumn();

                dc.ColumnName = col.Name;
                dc.Caption = col.DisplayName;
                dc.DataType = col.DataType;
                dc.ExtendedProperties.Add(col.Name, col);
                dc.Visible = col.Enabled;
                dt.Columns.Add(dc);
            }
            else
                foreach (Column c in col.SubColumns.Values)
                {
                    AddAllSubCols(dt, c);
                }
        }

        #endregion

        public StandardTableFormatter()
        {
            this.emitter = new List<ColumnEmitter>();
        }
    }


    public class XemlDataColumn : DataColumn
    {
        private bool _visible;

        public bool Visible
        {
            get { return _visible; }

            set { _visible = value; }
        }

    }
}
